﻿// Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
// 作者:吕艳阳

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Helper;

namespace XCI.Setting
{
    /// <summary>
    /// 配置文件管理器
    /// </summary>
    public class SettingStoreManager
    {
        public ISettingStore SettingStore { get; set; }
        public ISettingSerializer SettingSerializer { get; }
        public string Id { get; }

        public SettingStoreManager(ISettingSerializer settingSerializer, string id) : this(settingSerializer, null, id)
        {

        }

        public SettingStoreManager(ISettingSerializer settingSerializer, ISettingStore settingStore, string id)
        {
            SettingSerializer = settingSerializer ?? throw new ArgumentNullException(nameof(settingSerializer));
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException(nameof(id));

            SettingStore = settingStore ?? App.SettingStore;
            Id = id;
        }

        /// <summary>
        /// 加载当前配置
        /// </summary>
        public Task LoadCurrentAsync()
        {
            return LoadAsync(AppDefaultSettings.LayoutStoreCurrentName);
        }

        /// <summary>
        /// 加载默认配置
        /// </summary>
        public Task LoadDefaultAsync()
        {
            return LoadAsync(AppDefaultSettings.LayoutStoreDefaultName);
        }

        /// <summary>
        /// 加载配置
        /// </summary>
        public Task LoadAsync(string name)
        {
            return SettingStore.LoadAsync(SettingSerializer, Id, name);
        }

        /// <summary>
        /// 保存当前配置
        /// </summary>
        public Task SaveCurrentAsync()
        {
            return SaveAsync(AppDefaultSettings.LayoutStoreCurrentName);
        }

        /// <summary>
        /// 保存默认配置
        /// </summary>
        public Task SaveDefaultAsync()
        {
            return SaveAsync(AppDefaultSettings.LayoutStoreDefaultName);
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        public Task SaveAsync(string name)
        {
            return SettingStore.SaveAsync(SettingSerializer, Id, name);
        }

        /// <summary>
        /// 下载配置
        /// </summary>
        /// <param name="name">配置名称</param>
        public Task DownloadAsync(string name)
        {
            return SettingStore.DownloadAsync(Id, name);
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        public Task DeleteAsync(string name)
        {
            return SettingStore.DeleteAsync(Id, name);
        }

        /// <summary>
        /// 删除所有配置
        /// </summary>
        public async Task DeleteAllAsync()
        {
            var list = await SettingStore.ListAsync(Id);
            foreach (var name in list)
            {
                await SettingStore.DeleteAsync(Id, name);
            }

            //删除本地配置文件夹
            var sfile = SettingStore.CrreateServerFile(Id, "name");
            Directory.Delete(sfile.LocalCatalog, true);
        }

        /// <summary>
        /// 复制配置
        /// </summary>
        /// <param name="sourceName">源名称</param>
        /// <param name="destName">目标名称</param>
        public Task CopyAsync(string sourceName, string destName)
        {
            return SettingStore.CopyAsync(Id, sourceName, destName);
        }

        /// <summary>
        /// 创建服务器文件
        /// </summary>
        /// <param name="name">配置名称</param>
        public ServerFile CrreateServerFile(string name)
        {
            return SettingStore.CrreateServerFile(Id, name);
        }

        /// <summary>
        /// 查询所有配置
        /// </summary>
        public Task<IList<string>> ListAsync()
        {
            return SettingStore.ListAsync(Id);
        }
    }
}